package com.hzit.auth.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */

@AllArgsConstructor

public class LoginUser implements UserDetails {
	private String username;                // 用户名
	private String password;                // 密码
	private boolean accountNonExpired;      // 账户是否到期
	private boolean accountNonLocked;       // 账户是否锁定
	private boolean enabled;                // 账户是否启用
	private boolean credentialsNonExpired;  // 凭证是否到期
	private List<String> permissions;       // 用户权限串列表

	private Collection<? extends GrantedAuthority> authorities;     // 用户权限列表

	public LoginUser(String username, String password, List<String> permissions) {
		this.username = username;
		this.password = password;
		this.permissions = permissions;
	}

	@Override
	public Collection<? extends GrantedAuthority> getAuthorities() {
		//将用户的权限串列表转换为Collection<? extends GrantedAuthority>
		return permissions.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
	}

	public List<String> getPermissions() {
		return permissions;
	}

	@Override
	public String getUsername() {
		return username;
	}

	@Override
	public String getPassword() {
		return password;
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}


	@Override
	public boolean isAccountNonLocked() {
		return true;
	}

	@Override
	public boolean isEnabled() {
		return true;
	}


	@Override
	public boolean isCredentialsNonExpired() {
		return true;
	}

}
